package com.egao.common.publicly.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egao.common.core.web.JsonResult;
import com.egao.common.publicly.entity.CalendarVO;
import com.egao.common.publicly.entity.PubliclyCareerVO;
import com.egao.common.publicly.mapper.PubliclyCalendarMapper;
import com.egao.common.publicly.service.PubliclyCalendarService;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 门户日历接口实现类
 *
 * @author pyx
 */
@Service
public class PubliclyCalendarServiceImpl extends ServiceImpl<PubliclyCalendarMapper, PubliclyCareerVO> implements PubliclyCalendarService {

    @Override
    public JsonResult listCareerCalendarMonth(String dateStr) {
        if(StrUtil.isBlank(dateStr)){
            return JsonResult.error("没有获取到日期");
        }

        Date date = DateUtil.parse(dateStr);
        Date beginOfMonth = DateUtil.beginOfMonth(date);
        Date endOfMonth = DateUtil.endOfMonth(date);

        ArrayList<HashMap<Integer, List<PubliclyCareerVO>>> hashMaps = new ArrayList<>();

        List<PubliclyCareerVO> publiclyCareerList = baseMapper.listCareer(beginOfMonth,endOfMonth);

        //处理迭代器防止为companyVOS为null
        List<PubliclyCareerVO> copyPubliclyCareerList = new ArrayList<>();
        copyPubliclyCareerList.addAll(publiclyCareerList);

        for (PubliclyCareerVO careerVO : publiclyCareerList) {

            if (careerVO.getHoldTime() != null) {
                List<PubliclyCareerVO> careerList = new ArrayList<>();
                HashMap<Integer, List<PubliclyCareerVO>> hashMap = new HashMap<>();
                Iterator<PubliclyCareerVO> iterator = copyPubliclyCareerList.iterator();

                while (iterator.hasNext()) {
                    PubliclyCareerVO career = iterator.next();
                    if (career.getDay().equals(careerVO.getDay())) {
                        careerList.add(career);
                        iterator.remove();
                    }
                }

                if (careerList != null && careerList.size() > 0) {
                    hashMap.put(DateUtil.dayOfMonth(careerVO.getHoldTime()), careerList);
                    hashMaps.add(hashMap);
                }

            }
        }
        return JsonResult.ok().setData(hashMaps);
    }


    @Override
    public JsonResult listCareerCalendarDay(String dateStr) {
        if(StrUtil.isBlank(dateStr)){
            return JsonResult.error("没有获取到日期");
        }

        Date date = DateUtil.parse(dateStr);
        Date beginOfDay = DateUtil.beginOfDay(date);
        Date endOfDay = DateUtil.endOfDay(date);

        List<PubliclyCareerVO> publiclyCareerList = baseMapper.listCareer(beginOfDay,endOfDay);
        return JsonResult.ok().setData(publiclyCareerList);
    }
}
